﻿
Imports System.Drawing
Partial Class chord
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim size As Integer

        'validate query string parameters

        'check that all three parameters are present
        If Request.QueryString("size") Is Nothing Or _
            Request.QueryString("chordPattern") Is Nothing Or _
            Request.QueryString("chordTitle") Is Nothing _
            Then
            Response.Redirect("~/images/error.png", False)
            Exit Sub
        End If

        'check pattern of size and chordPattern parameters in query string

        If Not Regex.IsMatch(Request.QueryString("size"), "^[0-9]{1,}$") Then
            'size parameter in query string is not an integer so redirect to "error" image
            Response.Redirect("~/images/error.png", False)
            Exit Sub
        End If
        size = CInt(Request.QueryString("size"))

        If Not Regex.IsMatch(Request.QueryString("chordPattern"), "^([0-9]{2}|x{2}){6}$") Then
            'chordPatter parameter in query string is not in the correct format so redirect to "error" image
            Response.Redirect("~/images/error.png", False)
            Exit Sub
        End If

        'invoke renderChord function to render the chord to a bitmap
        Dim chordBitmap As Bitmap = renderer.renderChord(Request.QueryString("chordPattern"), System.Web.HttpUtility.UrlDecode(Request.QueryString("chordTitle")), size)

        'save bitmap to HTML output stream
        chordBitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png)
    End Sub
End Class
